package com.easy.customer.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.easy.common.bean.CustomerContacts;
import com.easy.common.bean.Customers;
import com.easy.common.service.ICustomersService;
import com.easy.common.util.SYSUtil;
import com.easy.customer.mapper.CustomerContactsMapper;
import com.easy.customer.mapper.CustomersMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CustomersService implements ICustomersService {
    @Autowired
    CustomersMapper customerMapper;
    @Autowired
    private CustomerContactsMapper contactsMapper;

    @Override
    @Transactional
    public Customers insert(Customers customer) {
        customerMapper.insert(customer);
        List<CustomerContacts> list=customer.getContacts();
        if(SYSUtil.checkNull(list)&&!list.isEmpty()){
            for(CustomerContacts item:list){
                item.setCustomer_id(customer.getId());
                contactsMapper.insert(item);
            }
        }
        Customers result=customerMapper.selectById(customer.getId());
        return result;
    }

    @Override
    public Customers update(Customers customer) {
        customerMapper.updateById(customer);
        Customers result=this.getByID(customer.getId());
        return result;
    }

    @Override
    public int delete(Integer id) {
        int num=customerMapper.deleteById(id);
        return num;
    }

    @Override
    public Customers getByID(Integer id) {
        Customers result=customerMapper.selectById(id);
        return result;
    }

    @Override
    public IPage<Customers> getListByPage(IPage page, Customers customer) {

        Wrapper wrapper=new QueryWrapper(customer);
        IPage result=customerMapper.selectPage(page,wrapper);
        return result;
    }

    @Override
    public List<Customers> getList(Customers customer) {
        Wrapper wrapper=new QueryWrapper(customer);
        List<Customers> result=customerMapper.selectList(wrapper);
        return result;
    }
}
